home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / AUTO_PTR.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  627b  |  36 lines

  1. #include <iostream.h>
  2. #include <memory>
  3.  
  4.  using namespace std;
  5.  
  6. //
  7. // A simple structure.
  8. //
  9.  
  10. struct X
  11. {
  12.     X (int i = 0) : m_i(i) { }
  13.     int get() const { return m_i; }
  14.     int m_i;
  15. };
  16.  
  17. int main ()
  18. {
  19.     //
  20.     // b will hold a pointer to an X.
  21.     //
  22.     auto_ptr<X> b(new X(12345));
  23.     //
  24.     // a will now be the owner of the underlying pointer.
  25.     //
  26.     auto_ptr<X> a = b;
  27.     //
  28.     // Output the value contained by the underlying pointer.
  29.     //
  30.     cout << a->get() << endl;
  31.     //
  32.     // The pointer will be delete'd when a is destroyed on leaving scope.
  33.     //
  34.     return 0;
  35. }
  36.